<?php
/**
 * Identify the browser, the version of the browser, the operation system
 * and the version of the operation system.
 *
 * Supporing browsers(14):
 * - Flock
 * - Firefox
 * - Opera
 * - Safari
 * - OmniWeb
 * - Internet Explorer
 * - Netscape
 * - SeaMonkey
 * - Konqueror
 * - Camino
 * - iCab
 * - Epiphany
 * - IceWeasel
 * - K-Meleon
 *
 * Supporting operation system(3):
 * - Mac(2)
 *  - OS X Intel
 * 	- OS X PPC
 * - Windows(7)
 * 	- 95
 * 	- 98
 * 	- 2000
 * 	- 2000 SP1
 * 	- XP
 * 	- Server 2003
 * 	- Vista
 * - Linux(4)
 * 	- Debian
 * 	- Ubuntu
 * 	- Fedora
 * 	- SUSE
 *
 * @author Cyril Gabathuler
 * @version 1.0 beta
 * @package com.tardo.util.UserAgentInfos
 *
 */
class UserAgentInfos {

	/**
	 * @var useragent The useragent string
	 */
	private $useragent;
	/**
	 * @var browser Name of the browser
	 */
	private $browser = "unidentified";
	/**
	 * @var browser_version Version of the browser
	 */
	private $browser_version = "unidentified";
	/**
	 * @var operation_system Name of the operation system
	 */
	private $operation_system = "unidentified";
	/**
	 * @var os_version Version of the operation system
	 */
	private $os_version = "unidentified";

	function __construct($useragent) {
		$this->useragent = $useragent;
	}

	/**
	 * This function searchs in the useragent string for a known browser
	 *
	 * @return return_browser An array with the name and the version of the browser
	 */
	function getBrowser() {
		if(eregi('flock', $this->useragent)) {
			//Flock has to be before Firefox
			$this->setBrowserDetails('Flock');
		} else if(eregi('Epiphany', $this->useragent)) {
			//Flock has to be before Firefox
			$this->setBrowserDetails('Epiphany');
		} else if(eregi('firefox', $this->useragent) || eregi('GranParadiso', $this->useragent) || eregi('Phoenix', $this->useragent)) {
			$this->setFirefox();
		} else if(eregi('opera', $this->useragent)) {
			$this->setBrowserDetails('Opera');
		} else if(eregi('OmniWeb', $this->useragent)) {
			//OmniWeb has to be before Safari!!
			$this->setOmniWeb();
		} else if(eregi('safari', $this->useragent)) {
			$this->setSafari();
		} else if(eregi('Internet Explorer', $this->useragent) || eregi('MSIE', $this->useragent)) {
			$this->setIE();
		} else if(eregi('Netscape', $this->useragent)) {
			$this->setBrowserDetails('Netscape');
		} else if(eregi('SeaMonkey', $this->useragent)) {
			$this->setBrowserDetails('SeaMonkey');
		} else if(eregi('Konqueror', $this->useragent)) {
			$this->setBrowserDetails('Konqueror');
		} else if(eregi('Camino', $this->useragent)) {
			$this->setBrowserDetails('Camino');
		} else if(eregi('iCab', $this->useragent)) {
			$this->setBrowserDetails('iCab');
		} else if(eregi('IceWeasel', $this->useragent)) {
			$this->setBrowserDetails('IceWeasel');
		} else if(eregi('K-Meleon', $this->useragent)) {
			$this->setBrowserDetails('K-Meleon');
		}

		$return_browser = array();
		$return_browser['browser'] = $this->browser;
		$return_browser['browser_version'] = $this->browser_version;
		return $return_browser;
	}

	/**
	 * This function searchs in the useragent string for a known operation system
	 *
	 * @return return_os An array with the name and the version of the operation system
	 */

	function getOperationSystem() {
		if(eregi('mac', $this->useragent)) {
			$this->setMac();
		} else if(eregi('windows', $this->useragent)) {
			$this->setWindows();
		} else if(eregi('linux', $this->useragent)) {
			$this->setLinux();
		}

		$return_os = array();
		$return_browser['operation_system'] = $this->operation_system;
		$return_browser['os_version'] = $this->os_version;
		return $return_browser;
	}

	/**
	 * Set the browser name to Firefox and get the version from the function getVersion()
	 * Provides also support for:
	 * - Phoenix (Firefox 0.5)
	 * - GranParadiso (Firefox 3.0 Alpha 1)
	 */
	function setFirefox() {
		$this->browser = "Firefox";

		if(stristr($this->useragent, "Firefox") == true) {
			$version = stristr($this->useragent, "Firefox");
		}
		else if(stristr($this->useragent, "GranParadiso") == true) {
			$version =stristr($this->useragent, "GranParadiso");
		}
		else if(stristr($this->useragent, "Phoenix") == true) {
			$version = stristr($this->useragent, "Phoenix");
		}
		$this->browser_version = $this->getBrowserVersion($version);
	}

	/**
	 * Set the browser name to Safari and get the version from the function getVersion()
	 *
	 * Supported versions:
	 * - v125
	 * - v312
	 * - 2.0
	 * - 2.0.4
	 * - 3.0
	 *
	 */
	function setSafari() {
		$this->browser = "Safari";

		$version = stristr($this->useragent, "safari");
		$version = $this->getBrowserVersion($version);
		if($version == '125' || $version == '312') {
			$this->browser_version = 'v'.$version;
		} else if($version == '412'){
			$this->browser_version = "2.0";
		} else {
			$version = explode(".", $version);
			$version = $version [0];
			if($version == '419') {
				$this->browser_version = "2.0.4";
			} else if($version == '521') {
				$this->browser_version = "3.0";
			}
		}
	}

	/**
	 * Set the browser name to OmniWeb and get the version from the function getVersion()
	 *
	 * Supported Versions:
	 * - 5.1.1
	 * - 5.1.1 beta 1
	 */
	function setOmniWeb() {
		$this->browser = "OmniWeb";

		$version = stristr($this->useragent, "omniweb");
		$version = $this->getBrowserVersion($version);

		if($version == "v563.51") {
			$this->browser_version = "5.1.1";
		} else if($version == "v563.57") {
			$this->browser_version = "5.1.2 beta 1";
		}
	}

	/**
	 * Set the browser name to IE and get the version from the function getVersion()
	 * Supports also Internet Explorer 1.0
	 */
	function setIE() {
		$this->browser = "Internet Explorer";

		if(stristr($this->useragent, "Internet Explorer") == true) {
			$this->browser_version = "1.0";
		} else if(stristr($this->useragent, "MSIE") == true) {
			$version = stristr($this->useragent, "MSIE");
			$this->browser_version = $this->getBrowserVersion($version);
		}
	}

	/**
	 * Set browser details dynamically via the parameter
	 *
	 * @var name Name of the browser
	 */
	 function setBrowserDetails($name) {
		$this->browser = $name;

		$version = stristr($this->useragent, $name);
		$this->browser_version = $this->getBrowserVersion($version);

	}

	/**
	 * Set the OS to Mac and set the version.
	 * It also supports the CPU of the OS.
	 * Supported CPU:
	 * - PPC or PowerPC
	 * - Intel
	 */
	function setMac() {
		$this->operation_system = "Mac";
		$version = '';
		if(stristr($this->useragent, "OS X") == true) {
			$version = stristr($this->useragent, "OS X");
			$version = explode(" ", $version);
			if(count(explode(";", $version['1'])) > 1) {
				$part1 = $version['0'];
				$part2 = explode(";", $version['1']);
				$version = $part1." ".$part2['0'];
			} else {
				$version = $version['0']." ".$version['1'];
			}
		}

		if(eregi("PPC", $this->useragent) || eregi("PowerPC", $this->useragent)) {
			$cpu = stristr($this->useragent, "PPC");
			if($cpu == '')
				$cpu = stristr($this->useragent, "PowerPC");
			$version = $version." ".$this->getMacCpu($cpu);
			$this->os_version = $version;
		} else if(eregi("Intel", $this->useragent)) {
			$cpu = stristr($this->useragent, "Intel");
			$version = $version." ".$this->getMacCpu($cpu);
			$this->os_version = $version;
		}
	}

	/**
	 * Set the OS to Windows and set the version.
	 */
	function setWindows() {
		$this->operation_system = "Windows";

		if(eregi("Windows 95", $this->useragent)) {
			$this->os_version = "95";
		} else if(eregi("Windows 98", $this->useragent)) {
			$this->os_version = "98";
		} else if(eregi("Windows NT 5.0", $this->useragent) || eregi("Windows 2000", $this->useragent)) {
			$this->os_version = "2000";
		} else if(eregi("Windows NT 5.01", $this->useragent)) {
			$this->os_version = "2000 SP1";
		} else if(eregi("Windows NT 5.1", $this->useragent) || eregi("Win32", $this->useragent)) {
			$this->os_version = "XP";
		} else if(eregi("Windows NT 5.2", $this->useragent)) {
			$this->os_version = "Server 2003";
		} else if(eregi("Windows NT 6.0", $this->useragent)) {
			$this->os_version = "Vista";
		}
	}

	/**
	 * Set the OS to Linux and set the version.
	 */
	function setLinux() {
		$this->operation_system = "Linux";

		if(eregi("debian", $this->useragent)) {
			$this->os_version = "Debian";
		} else if(eregi("ubuntu", $this->useragent)) {
			$this->os_version = "Ubuntu";
		} else if(eregi("fedora", $this->useragent)) {
			$this->os_version = "Fedora";
		} else if(eregi("suse", $this->useragent)) {
			$this->os_version = "SuSE";
		}
	}

	/**
	 * This function gets a string, which contains the version of the browser,
	 * but the string must be seperated
	 *
	 * Posibilities:
	 *
	 * - trash/[version] trash
	 * - trash/[version];trash
	 * - trash [version] trash
	 * - trash [version];trash
	 *
	 * @var string A string, which contains the version
	 * @return version The seperated version of the browser
	 */
	function getBrowserVersion($string) {
		$version = explode("/", $string);
		if(count($version)<=1) {
			$version = explode(" ", $version['0']);
		}
		$version = explode(" ", $version['1']);
		if(count($version)>=1) {
			$version = explode(";", $version['0']);
		}
		return $version['0'];
	}

	/**
	 * This function gets a string, which contains the cpu of the mac os,
	 * but the string must be seperated
	 *
	 * Posibilities:
	 *
	 * - [cpu] trash
	 * - [cpu];trash
	 *
	 * @var cpuString A string, which contains the cpu
	 * @return version The seperated cpu of the mac os
	 */
	function getMacCpu($cpuString) {
		$cpu = explode(" ", $cpuString);
		if(eregi(";", $cpu['0'])) {
			$cpu = explode(";", $cpu['0']);
		} else if(eregi(")", $cpu['0'])) {
			$cpu = explode(")", $cpu['0']);
		}

		return $cpu['0'];
	}

}

?>